/**
 * @author：anlzou
 * @Date：Created in9:56 2021/4/19
 * @Github：https://github.com/anlzou
 * @Description：
 */
package com.anlzou.threads;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Threads3 implements Callable<Object> {
    @Override
    public Object call() throws Exception {
        System.out.println("通过Callable实现多线程、名称："+Thread.currentThread().getName());
        return "这是返回值";
    }

    public void createThread(){
        FutureTask<Object> futureTask = new FutureTask<>(new Threads3());

        //FutureTask继承了Runnable，可以放在Thread中启动执行
        Thread thread = new Thread(futureTask);
        thread.setName("demo3");
        thread.start();
        System.out.println("主线程名称:"+Thread.currentThread().getName());

        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            //阻塞等待中被中断则抛出
            e.printStackTrace();
        } catch (ExecutionException e) {
            //执行过程发送异常被抛出
            e.printStackTrace();
        }
    }
}
